# -*- coding:utf-8 -*-
import logging
import os
import sys
from logging.handlers import TimedRotatingFileHandler
from flask.logging import default_handler

login_level=logging.INFO   #根据需求，可以logging.DEBUG
def setting_logging(app, logs_path, envValue):
    # root = logging.getLogger()
    # root.addHandler(default_handler)
    root = logging.getLogger()
    root.setLevel(login_level)
    app.logger.removeHandler(default_handler)
    formatter = '[%(asctime)s] %(levelname)s -- %(filename)s %(funcName)s thread:%(thread)d [line:%(lineno)d]\n-> %(message)s'
    console_handler = logging.StreamHandler()
    console_handler.setLevel(login_level)
    console_handler.setFormatter(logging.Formatter(formatter))
    console_handler.setStream(sys.stdout)
    #app.logger.addHandler(console_handler)
    root.addHandler(console_handler)

    if envValue != 'dev':
        filename = os.path.join(logs_path, 'app.log')
        timed_rotating_file_handler = TimedRotatingFileHandler(filename=filename, when='midnight', interval=1, backupCount=14)  #MIDNIGHT 0点开始生成新文件 保留14天
        timed_rotating_file_handler.setLevel(login_level)
        timed_rotating_file_handler.setFormatter(logging.Formatter(formatter))
        root.addHandler(timed_rotating_file_handler)
        #app.logger.addHandler(timed_rotating_file_handler)
